<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>栅格分段专题图</title>

    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/openlayers/4.2.0/ol.css" rel="stylesheet">
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>

<script src="http://cdn.bootcss.com/openlayers/4.2.0/ol.js"></script>
<script type="text/javascript" src="../../dist/iclient9-openlayers.min.js"></script>
<script type="text/javascript">
    var map, themeLayer, options,
        url = "http://support.supermap.com.cn:8090/iserver/services/map-jingjin/rest/maps/京津地区人口分布图_专题图";
    new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [117, 40],
                zoom: 7,
                projection: "EPSG:4326"
            })
        });
        options = ol.source.TileSuperMapRest.optionsFromMapJSON(url, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        createTheme();
    });

    function createTheme() {
        var themeGridRangeItem1 = new SuperMap.ThemeGridRangeItem({
                start: -4,
                end: 120,
                color: new SuperMap.ServerColor(198, 244, 240)
            }),

            themeGridRangeItem2 = new SuperMap.ThemeGridRangeItem({
                start: 120,
                end: 240,
                color: new SuperMap.ServerColor(176, 244, 188)
            }),
            themeGridRangeItem3 = new SuperMap.ThemeGridRangeItem({
                start: 240,
                end: 360,
                color: new SuperMap.ServerColor(218, 251, 178)
            }),
            themeGridRangeItem4 = new SuperMap.ThemeGridRangeItem({
                start: 360,
                end: 480,
                color: new SuperMap.ServerColor(220, 236, 145)
            }),
            themeGridRangeItem5 = new SuperMap.ThemeGridRangeItem({
                start: 480,
                end: 600,
                color: new SuperMap.ServerColor(96, 198, 66)
            }),
            themeGridRangeItem6 = new SuperMap.ThemeGridRangeItem({
                start: 600,
                end: 720,
                color: new SuperMap.ServerColor(20, 142, 53)
            }),
            themeGridRangeItem7 = new SuperMap.ThemeGridRangeItem({
                start: 720,
                end: 840,
                color: new SuperMap.ServerColor(85, 144, 55)
            }),

            themeGridRangeItem8 = new SuperMap.ThemeGridRangeItem({
                start: 840,
                end: 960,
                color: new SuperMap.ServerColor(171, 168, 38)
            }),
            themeGridRangeItem9 = new SuperMap.ThemeGridRangeItem({
                start: 960,
                end: 1100,
                color: new SuperMap.ServerColor(235, 165, 9)
            }),
            themeGridRangeItem10 = new SuperMap.ThemeGridRangeItem({
                start: 1100,
                end: 1220,
                color: new SuperMap.ServerColor(203, 89, 2)
            }),
            themeGridRangeItem11 = new SuperMap.ThemeGridRangeItem({
                start: 1220,
                end: 1340,
                color: new SuperMap.ServerColor(157, 25, 1)
            }),
            themeGridRangeItem12 = new SuperMap.ThemeGridRangeItem({
                start: 1340,
                end: 1460,
                color: new SuperMap.ServerColor(118, 15, 3)
            }),
            themeGridRangeItem13 = new SuperMap.ThemeGridRangeItem({
                start: 1460,
                end: 1600,
                color: new SuperMap.ServerColor(112, 32, 7)
            }),

            themeGridRangeItem14 = new SuperMap.ThemeGridRangeItem({
                start: 1600,
                end: 1800,
                color: new SuperMap.ServerColor(106, 45, 12)
            }),
            themeGridRangeItem15 = new SuperMap.ThemeGridRangeItem({
                start: 1800,
                end: 2000,
                color: new SuperMap.ServerColor(129, 80, 50)
            }),
            themeGridRangeItem16 = new SuperMap.ThemeGridRangeItem({
                start: 2000,
                end: 2167,
                color: new SuperMap.ServerColor(160, 154, 146)
            });
        var themeGridRange = new SuperMap.ThemeGridRange({
            reverseColor: false,
            rangeMode: SuperMap.RangeMode.EQUALINTERVAL,
            items: [
                themeGridRangeItem1, themeGridRangeItem2,
                themeGridRangeItem3, themeGridRangeItem4,
                themeGridRangeItem5, themeGridRangeItem6,
                themeGridRangeItem7, themeGridRangeItem8,
                themeGridRangeItem9, themeGridRangeItem10,
                themeGridRangeItem11, themeGridRangeItem12,
                themeGridRangeItem13, themeGridRangeItem14,
                themeGridRangeItem15, themeGridRangeItem16
            ]
        });
        var themeParameters = new SuperMap.ThemeParameters({
            datasetNames: ["JingjinTerrain"],
            dataSourceNames: ["Jingjin"],
            joinItems: null,
            themes: [themeGridRange]
        });

        var extent = [114.59, 37.76, 119.51, 42.31];
        new ol.supermap.ThemeService(url).getThemeInfo(themeParameters, function (serviceResult) {
            var result = serviceResult.result;
            if (result && result.newResourceID) {
                themeLayer = new ol.layer.Tile({
                    source: new ol.source.TileSuperMapRest({
                        url: url,
                        noWrap: true,
                        cacheEnabled: false,
                        layersID: result.newResourceID,
                        tileGrid: new ol.tilegrid.TileGrid({
                            extent: extent,
                            resolutions: options.tileGrid.getResolutions()
                        }),
                        transparent: true,
                    })
                });
                map.addLayer(themeLayer);
            }
        });
    }
</script>
</body>
</html>